<?php 
class profile
{
	public $db;
	public $id;
	public $user_id;
	public $address;
	public $city_id;
	public $city;
	public $details;
	function __construct() 
	{
		$this->db = new database();
	}
	function toDB($id = null)
	{
		if(!$id) {			$insert_id = $this->db->insert("INSERT INTO `profile` VALUES(null, '".$this->city_id."', '".$this->user_id."', '".$this->address."', 0, 0, '".$this->details."')");						if(!$insert_id) {				return false;			} else {				return $insert_id;			}
		} else {
			if($this->db->update("UPDATE `profile` SET `address`='".$this->address."', `city_id`='".$this->city_id."', `details`='".$this->details."' WHERE `id`='".$this->id."'")) {
				return true;
			} else {
				return false;
			}
		}
	}
	function load($id, $user_id)
	{

		$result = $this->db->select("SELECT `p`.`address` AS 'address', `p`.`details` AS 'details', `c`.`ID` AS 'city_id', `c`.`name` AS 'city' FROM `profile` AS `p`, `city` AS `c` WHERE `p`.`city_id` = `c`.`ID` AND `p`.`ID`='".$id."' AND `p`.`user_ID` = '".$user_id."'");

		if(mysql_num_rows($result) != 1) {

			return false;
		}

		else {
			$row = mysql_fetch_array($result);

			$this->id = $id;
			$this->address = $row['address'];
			$this->city_id = $row['city_id'];
			$this->city = $row['city'];
			$this->details = $row['details'];

			return true;
		}
	}
	
	function loadAdmin($id)
	{

		$result = $this->db->select("SELECT `p`.`address` AS 'address', `p`.`details` AS 'details', `c`.`ID` AS 'city_id', `c`.`name` AS 'city' FROM `profile` AS `p`, `city` AS `c` WHERE `p`.`city_id` = `c`.`ID` AND `p`.`ID`='".$id."'");

		if(mysql_num_rows($result) != 1) {

			return false;
		}

		else {
			$row = mysql_fetch_array($result);

			$this->id = $id;
			$this->address = $row['address'];
			$this->city_id = $row['city_id'];
			$this->city = $row['city'];
			$this->details = $row['details'];

			return true;
		}
	}		function fromArray($row)	{		$this->id = $row['ID'];		$this->address = $row['address'];		$this->city_id = $row['city_id'];		$this->city = $row['city'];		$this->details = $row['details'];	}
	function validateField($field, $value)
	{
		switch ($field) {
			case "address":
				if($value == "") {
					return false;
				} else {
					return true;
				}
			break;
			case "city":
				if(!$this->db->exist("SELECT * FROM `city` WHERE `id`='".$value."'")) {
					return false;
				} else {
					return true;
				}
			break;
		}
	}
}
?>